<template>
  <div>
    <bread-crumb name1="商品管理" name2="商品列表"></bread-crumb>
    <el-card>
      <el-row>
        <el-col :span="6">
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" @click="$router.push('/goodsinfo')"
            >添加商品</el-button
          >
        </el-col>
      </el-row>
      <!-- 数据渲染 -->
      <el-table :data="goodsInfoTableData" style="width: 100%" border>
        <el-table-column label="#" type="index"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="200">
        </el-table-column>
        <el-table-column prop="goods_price" label="价格（元）">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scoped">
            <div>
              <p>
                {{ $moment(scoped.row.add_time).format("YYYY-MM-DD,hh:mm:ss") }}
              </p>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作"> </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="paramsObj.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import BreadCrumb from "@/components/common/breadcrumb/BreadCrumb";
import { goods_list_req } from "@/api/goods.js";
export default {
  data() {
    return {
      paramsObj: {
        query: "",
        pagesize: 5,
        pagenum: 1,
      },
      // 表格数据的初始值
      goodsInfoTableData: [],
      // 总条数
      total: 0,
    };
  },
  components: {
    BreadCrumb,
  },
  mounted() {
    this.getGoodsList();
  },
  methods: {
    async getGoodsList() {
      const result = await goods_list_req(this.paramsObj);
      console.log(result);
      if (result.meta.status !== 200) {
        return this.$message.error("数据请求失败");
      }
      this.goodsInfoTableData = result.data.goods;
      this.total = result.data.total;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.paramsObj.pagesize = val;
      this.getGoodsList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.paramsObj.pagenum = val;
      this.getGoodsList();
    },
  },
};
</script>

<style lang="scss" scoped>
</style>